package com.gs.SynAntiWandering.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.base.BaseActivity;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.presenter.ForgotPwPresenter;
import com.gs.SynAntiWandering.util.ToastUtil;
import com.gs.SynAntiWandering.util.WindowUtil;
import com.gs.SynAntiWandering.view.IForgotPwView;

public class ForgotPwActivity extends BaseActivity<IForgotPwView, ForgotPwPresenter> implements IForgotPwView {

    private EditText etPw;
    private Button btnResetPw;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forgot_pw;
    }

    @Override
    protected void initView() {
        WindowUtil.setWhiteBarBg(this);
        OnClickListener onClickListener = new OnClickListener();
        btnResetPw = fd(R.id.btn_reset_pw);
        btnResetPw.setOnClickListener(onClickListener);
        fd(R.id.tv_back).setOnClickListener(onClickListener);
        etPw = fd(R.id.et_pw);
    }

    @Override
    protected ForgotPwPresenter createPresenter() {
        return new ForgotPwPresenter();
    }

    @Override
    public void per() {

    }

    @Override
    public String getEmail() {
        return etPw.getText().toString();
    }

    @Override
    public void onGetDataSuccess(ApiResultData<String> apiResult) {
        btnResetPw.setEnabled(true);
        ToastUtil.showToast(apiResult.getMessage());
        if (apiResult.getStatus() == 2000) {
            finish();
        }
    }

    @Override
    public void onGetDataFails(Exception e) {
        btnResetPw.setEnabled(true);
        ToastUtil.showToast(R.string.network_error);
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.btn_reset_pw) {
                if (TextUtils.isEmpty(getEmail())) {
                    ToastUtil.showToast(R.string.email_hint);
                    return;
                }
                v.setEnabled(false);
                mPresenter.forgotPw();
            } else if (id == R.id.tv_back) {
                finish();
            }
        }
    }
}